//
//  MKUser.swift
//  MOKOAnnounce
//
//  Created by 曹相召 on 2017/7/14.
//  Copyright © 2017年 cc.moko. All rights reserved.
//

import UIKit
import HandyJSON

enum MKUserType:Int ,HandyJSONEnum {
    case Actor = 1          //艺人
    case Businessmen = 2    //商家
    case Photographer = 3   //摄影师
    case ActorManager = 4   //经纪机构
}
class MKUser: NSObject {
    
    //用户的单例
    static let shareUser = MKUser()
    
    var userid:Int{
        set{
            UserDefaults.standard.set(userid, forKey: "userid")
            UserDefaults.standard.synchronize()
        }
        get{
            let userid = UserDefaults.standard.integer(forKey: "userid")
            return userid
        }
    }
    var userType:MKUserType{
        set{
            UserDefaults.standard.set(userid, forKey: "userType")
            UserDefaults.standard.synchronize()
        }
        get{
            let userType = UserDefaults.standard.integer(forKey: "userType")
            return MKUserType(rawValue: userType)!
        }
    }
    
    //是否登录
    func isLoginSuccess() -> Bool {
       let userType =  UserDefaults.standard.integer(forKey: "userType")
       let userid = UserDefaults.standard.integer(forKey: "userid")
       return userType > 0 && userid > 0
    }
    
    //保存用户的id
    func saveUserid(userid:Int) -> Void {
        UserDefaults.standard.set(userid, forKey: "userid")
        UserDefaults.standard.synchronize()
    }
    
    //保存用户的类型
    func saveUserType(userType:Int) -> Void {
        UserDefaults.standard.set(userType, forKey: "userType")
        UserDefaults.standard.synchronize()
    }
    
    //是否是商家端
    func isBusinessSide() -> Bool {
        let userType = UserDefaults.standard.integer(forKey: "userType")
        return userType == 2 || userType == 3
    }
    
    //移除用户信息
    func removeUser() -> Void {
        UserDefaults.standard.removeObject(forKey: "userType")
        UserDefaults.standard.removeObject(forKey: "userid")
        UserDefaults.standard.synchronize()
    }
}
